// pages/addaddress/addaddress.js
import address, { areas, citys, provinces } from "../../utils/city"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 判断地址是修改还是新增
    isShow: true,
    // 地址联动是否显示
    isBlock:false,
    // 省市区
    provinces:[],
    citys:[],
    areas:[],
    // 地址联动的value值
    value:[0,0,0],

    username:'',
    phone:'',
    textarea:'',
    detailsit:'',
    // 判断是新增地址还是编辑地址下标
    index:''
  },

  // 地区信息 输入框聚焦时显示地址联动
  isShowSite:function(){
    this.setData({
      isBlock:true
    })
  },

  // 点击取消和背景隐藏地址联动
  isHideSite:function(){
    this.setData({
      isBlock:false
    })
  },

  // 点击确定获取地区信息
  getSiteData:function(){
    var value = this.data .value
    var provinces = this.data.provinces[value[0]].name
    var citys = this.data.citys[value[1]].name
    var areas = this.data.areas[value[2]].name
 
    this.setData({
      isBlock:false,
      textarea:provinces+' '+citys+' '+areas
    })
  },

  // 获取输入的姓名
  getusername:function(event){
    this.setData({
      username:event.detail.value
    })
  },

  // 获取输入的手机号码
  getphone:function(event){
    this.setData({
      phone:event.detail.value
    })
  },

  // 获取输入的地址详情
  getdetailsite:function(event){
    this.setData({
      detailsite:event.detail.value
    })
  },

  // 地址联动
  cityChange:function(event){
    // console.log(event.detail.value)
    var value = event.detail.value
    var provincesIndex = value[0]
    var citysIndex = value[1]
    var areasIndex = value[2]
    if(provincesIndex != this.data.value[0]){
      var id1 = address.provinces[provincesIndex].id
      // console.log(id1)
      this.setData({
        value:[provincesIndex,0,0],
        citys:address.citys[id1],
        areas:address.areas[address.citys[id1][0].id]
      })
    }else if(citysIndex != this.data.value[1]){
      var id2 = this.data.citys[citysIndex].id
      // console.log(id2)
      this.setData({
        value:[provincesIndex,citysIndex,0],
        areas:address.areas[id2]
      })
    }else{
      this.setData({
        value:[provincesIndex,citysIndex,areasIndex]
      })
    }
    
  },

// 编辑地址中的保存并使用
 editAdd:function(){
  // 先把当前选中的地址删除
  var index = this.data.index
  var addressList = wx.getStorageSync('addressList')
  addressList.splice(index,1)
  // 再把修改后的地址添加到头部
  var item = {
    username:this.data.username,
    phone:this.data.phone,
    textarea:this.data.textarea,
    detailsite:this.data.detailsite,
  }
  if(addressList.length == 0){
    wx.setStorageSync('addressList', [item])
  }else{
    addressList.unshift(item)
    wx.setStorageSync('addressList', addressList)
  }
  wx.navigateBack({
    complete:(res)=>{},
  })
 },

// 编辑地址中的删除
delData:function(){
  var index = this.data.index
  var addressList = wx.getStorageSync('addressList')
  if(index != '-1'){
    addressList.splice(index,1)
    // console.log(addressList)
    wx.setStorageSync('addressList', addressList)
  }
  wx.navigateBack({
    complete:(res)=>{},
  })

  
},

// 新增地址中的保存并使用
addData:function(){
    // 将输入的用户信息添加到缓存数据中
    var item={
      username:this.data.username,
      phone:this.data.phone,
      textarea:this.data.textarea,
      detailsite:this.data.detailsite,
    }
    // console.log(item)
    var addressList = wx.getStorageSync('addressList')
    if(addressList.length==0){
      wx.setStorageSync('addressList', [item])
    }else{
      console.log(addressList)
      addressList.unshift(item);
      wx.setStorageSync('addressList', addressList)
    }
    wx.navigateBack({
      complete:(res)=>{},
    })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var index = options.index
    var isShow = true
    if (index == '-1') {
      isShow = true
      wx.setNavigationBarTitle({
        title: '新增收货地址',
      })
    } else {
      isShow = false
      wx.setNavigationBarTitle({
        title: '编辑收货地址',
      })
    }
    // 地址联动
    // console.log(address.provinces[0].id)
    // console.log(address.citys[address.provinces[0].id][0].id)
    // console.log(address.areas[address.citys[address.provinces[0].id][0].id])
    this.setData({
      isShow,
      index,
      // 设置省
      provinces:address.provinces,
      // 设置市
      citys:address.citys[address.provinces[0].id],
      // 设置区
      areas:address.areas[address.citys[address.provinces[0].id][0].id]
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(this.data.index)
    var index = this.data.index
    var addressList = wx.getStorageSync('addressList')
    // 如果不是新增地址，就把当前的数据显示在Input框中
    if(index != '-1'){
      this.setData({
        username:addressList[index].username,
        phone:addressList[index].phone,
        textarea:addressList[index].textarea,
        detailsite:addressList[index].detailsite
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})